package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> querySpecParams(Long cid, Long gid, Boolean search) {

        if (cid == null && gid == null) {

            throw new LyException(HttpStatus.BAD_REQUEST.value(),"查询条件不能为空！");

        }

        List<SpecParam> specParams = query()
                .eq(cid != null, "category_id", cid)
                .eq(gid != null, "group_id", gid)
                .eq(search != null, "searching", search)
                .list();

        return SpecParamDTO.convertEntityList(specParams);
    }

    @Override
    public void saveParam(SpecParamDTO specParamDTO) {

        this.save(specParamDTO.toEntity(SpecParam.class));

    }

    @Override
    public void updateParam(SpecParamDTO specParamDTO) {

        this.updateById(specParamDTO.toEntity(SpecParam.class));

    }

    @Override
    public void deleteParam(Long id) {

        this.removeById(id);

    }

}
